/*
************************************************************************************************
*  File     : Key_sweep.c
*
*  Driver for keyboard
*
*  Author   : wangwenbo
*  
*  Version  : 1.1
*
************************************************************************************************
*/

#include "lpc_types.h"
#include "key_sweep.h"
#include "lpc177x_8x_pinsel.h"
#include "lpc177x_8x_gpio.h"

volatile uint64_t  key;

/*
***********************************************************************************************
*    void key_init()
*
*    initializition	I/O
*
*    arguments : NONE
*               
*    return    : NONE
**********************************************************************************************
*/
void key_init(void)
{	

	PINSEL_ConfigPin(0, 17, 0);
	PINSEL_ConfigPin(0, 18, 0);
	PINSEL_ConfigPin(0, 19, 0);
	PINSEL_ConfigPin(0, 20, 0);
	PINSEL_ConfigPin(0, 21, 0);
	PINSEL_ConfigPin(0, 22, 0);
	PINSEL_ConfigPin(0, 5 , 0);
	PINSEL_ConfigPin(0, 4 , 0);
	PINSEL_ConfigPin(5, 2 , 0);
	PINSEL_ConfigPin(5, 3 , 0);
	PINSEL_ConfigPin(0, 15, 0);
	PINSEL_ConfigPin(0, 16, 0);

// SET DIR
	GPIO_SetDir(KEY_LINE_0_CONNECTED_PORT,KEY_LINE_0_CONNECTED_MASK,GPIO_DIRECTION_OUTPUT);
	GPIO_SetDir(KEY_LINE_1_CONNECTED_PORT,KEY_LINE_1_CONNECTED_MASK,GPIO_DIRECTION_OUTPUT);
	GPIO_SetDir(KEY_LINE_2_CONNECTED_PORT,KEY_LINE_2_CONNECTED_MASK,GPIO_DIRECTION_OUTPUT);
	
	GPIO_SetDir(KEY_SWEEP_0_CONNECTED_PORT,KEY_SWEEP_0_CONNECTED_MASK,GPIO_DIRECTION_INPUT);
	GPIO_SetDir(KEY_SWEEP_1_CONNECTED_PORT,KEY_SWEEP_1_CONNECTED_MASK,GPIO_DIRECTION_INPUT);
	GPIO_SetDir(KEY_SWEEP_2_CONNECTED_PORT,KEY_SWEEP_2_CONNECTED_MASK,GPIO_DIRECTION_INPUT);
	GPIO_SetDir(KEY_SWEEP_3_CONNECTED_PORT,KEY_SWEEP_3_CONNECTED_MASK,GPIO_DIRECTION_INPUT);
	GPIO_SetDir(KEY_SWEEP_4_CONNECTED_PORT,KEY_SWEEP_4_CONNECTED_MASK,GPIO_DIRECTION_INPUT);

}

/*
***********************************************************************************************
*    uint32_t get_key()
*
*    get key number
*
*    arguments : NONE
*               
*    return    : key_value
**********************************************************************************************
*/
uint64_t get_key(void)
{	
	uint32_t key1_value, key2_value,key_num1,key_num2;
	uint32_t i = 0;
	key = 0x00;

	for(i=0; i<=7; i++)
	{
	
		GPIO_OutputValue(KEY_LINE_0_CONNECTED_PORT,KEY_LINE_0_CONNECTED_MASK,i&0x01);
		GPIO_OutputValue(KEY_LINE_1_CONNECTED_PORT,KEY_LINE_1_CONNECTED_MASK,(i>>1)&0x01);
		GPIO_OutputValue(KEY_LINE_2_CONNECTED_PORT,KEY_LINE_2_CONNECTED_MASK,(i>>2)&0x01);
		
		key1_value = GPIO_ReadValue(0);
		key2_value = GPIO_ReadValue(5);
		key_num1 = key1_value & KEY1_VALUE_MASK;
		key_num2 = key2_value & KEY2_VALUE_MASK;
		
		if(key_num1!=KEY1_VALUE_MASK ||key_num2!=KEY2_VALUE_MASK)
		{
			if((key_num1&ROW1_VALUE)>>KEY_SWEEP_0_CONNECTED_PIN==0)
			{	
				key |= ((volatile uint64_t)0x01<<(0+5*i));
			}
			if((key_num1&ROW2_VALUE)>>KEY_SWEEP_1_CONNECTED_PIN==0)
			{	
				key |= ((volatile uint64_t)0x01<<(1+5*i));
			}
			if((key_num2&ROW3_VALUE)>>KEY_SWEEP_2_CONNECTED_PIN==0)
			{	
				key |= ((volatile uint64_t)0x01<<(2+5*i));
			}
			if((key_num2&ROW4_VALUE)>>KEY_SWEEP_3_CONNECTED_PIN==0)
			{	
				key |= ((volatile uint64_t)0x01<<(3+5*i));
			}
			if((key_num1&ROW5_VALUE)>>KEY_SWEEP_4_CONNECTED_PIN==0)
			{	
				key |= ((volatile uint64_t)0x01<<(4+5*i));
			}

		}

	}
	return key;
}
